﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;

namespace Squadron.Core
{
    public class AddinManager
    {
        public List<SquadronAddin> AddIns
        {
            get
            {
                if (_AddIns.Count == 0)
                    LoadAddins();

                return _AddIns;
            }
        }

        public List<SquadronAddin> _AddIns = new List<SquadronAddin>();

        internal void LoadAddins()
        {
            _AddIns.Clear();

            foreach (string file in Directory.GetFiles(GetExecutionFolder(), "*.dll"))
            {
                Assembly assembly = Assembly.LoadFile(file);
                foreach (Type type in assembly.GetExportedTypes())
                {
                    if (typeof(SquadronAddin).IsAssignableFrom(type))
                    {
                        object instance = Activator.CreateInstance(type);

                        _AddIns.Add(instance as SquadronAddin);
                    }
                }
            }
        }

        public static string GetExecutionFolder()
        {
            return Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
        }
    }
}
